/**
 * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.opsli.modulars.tenant.customerstatus.web;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.opsli.api.base.result.ResultWrapper;
import org.opsli.api.web.tenant.customerstatus.TenantCustomerStatusRestApi;
import org.opsli.api.wrapper.tenant.customerstatus.TenantCustomerStatusModel;
import org.opsli.common.annotation.ApiRestController;
import org.opsli.core.base.controller.BaseRestController;
import org.opsli.core.log.annotation.OperateLogger;
import org.opsli.core.log.enums.ModuleEnum;
import org.opsli.core.log.enums.OperationTypeEnum;
import org.opsli.core.persistence.Page;
import org.opsli.core.persistence.querybuilder.QueryBuilder;
import org.opsli.core.persistence.querybuilder.WebQueryBuilder;
import org.opsli.modulars.tenant.customerstatus.entity.TenantCustomerStatus;
import org.opsli.modulars.tenant.customerstatus.service.ITenantCustomerStatusService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Optional;

/**
 * 租户客户状态配置 Controller
 *
 * @author guagua
 * @date 2025-01-04
 */
@Api(tags = TenantCustomerStatusRestApi.TITLE)
@Slf4j
@ApiRestController("/{ver}/tenant/customerstatus")
public class TenantCustomerStatusRestController
        extends BaseRestController<TenantCustomerStatus, TenantCustomerStatusModel, ITenantCustomerStatusService>
        implements TenantCustomerStatusRestApi {

    /**
     * 客户状态配置 查一条
     *
     * @param model 模型
     * @return ResultWrapper
     */
    @ApiOperation(value = "获得单条客户状态配置", notes = "获得单条客户状态配置 - ID")
    @PreAuthorize("hasAuthority('tenant_customerstatus_select')")
    @Override
    public ResultWrapper<TenantCustomerStatusModel> get(TenantCustomerStatusModel model) {
        if (model != null && model.getIzApi() != null && model.getIzApi()) {
            model = IService.get(model);
        }
        return ResultWrapper.getSuccessResultWrapper(model);
    }

    /**
     * 客户状态配置 查询分页
     *
     * @param pageNo   当前页
     * @param pageSize 每页条数
     * @param request  request
     * @return ResultWrapper
     */
    @ApiOperation(value = "获得分页数据", notes = "获得分页数据 - 查询构造器")
    @PreAuthorize("hasAuthority('tenant_customerstatus_select')")
    @Override
    public ResultWrapper<?> findPage(Integer pageNo, Integer pageSize, HttpServletRequest request) {
        QueryBuilder<TenantCustomerStatus> queryBuilder = new WebQueryBuilder<>(IService.getEntityClass(),
                request.getParameterMap());
        Page<TenantCustomerStatus, TenantCustomerStatusModel> page = new Page<>(pageNo, pageSize);
        page.setQueryWrapper(queryBuilder.build());
        page = IService.findPage(page);
        return ResultWrapper.getSuccessResultWrapper(page.getPageData());
    }

    /**
     * 获取所有有效状态列表
     *
     * @return ResultWrapper
     */
    @ApiOperation(value = "获取所有有效状态列表", notes = "获取所有有效状态列表")
    @PreAuthorize("hasAuthority('tenant_customerstatus_select')")
    @Override
    public ResultWrapper<?> findAll() {
        List<TenantCustomerStatusModel> list = IService.findAllActive();
        return ResultWrapper.getSuccessResultWrapper(list);
    }

    /**
     * 客户状态配置 新增
     *
     * @param model 模型
     * @return ResultWrapper
     */
    @ApiOperation(value = "新增客户状态配置数据", notes = "新增客户状态配置数据")
    @PreAuthorize("hasAuthority('tenant_customerstatus_insert')")
    @OperateLogger(description = "新增客户状态配置数据", module = ModuleEnum.MODULE_TENANT, operationType = OperationTypeEnum.INSERT, db = true)
    @Override
    public ResultWrapper<?> insert(TenantCustomerStatusModel model) {

        QueryWrapper q = new QueryWrapper();
        q.eq("status_code", model.getStatusCode());
        if (IService.getOne(q) != null) {
            return ResultWrapper.getCustomResultWrapper(500, "状态码已存在，请勿重复添加");
        }
        IService.insert(model);
        return ResultWrapper.getSuccessResultWrapperByMsg("新增客户状态配置成功");
    }

    /**
     * 客户状态配置 修改
     *
     * @param model 模型
     * @return ResultWrapper
     */
    @ApiOperation(value = "修改客户状态配置数据", notes = "修改客户状态配置数据")
    @PreAuthorize("hasAuthority('tenant_customerstatus_update')")
    @OperateLogger(description = "修改客户状态配置数据", module = ModuleEnum.MODULE_TENANT, operationType = OperationTypeEnum.UPDATE, db = true)
    @Override
    public ResultWrapper<?> update(TenantCustomerStatusModel model) {
        IService.update(model);
        return ResultWrapper.getSuccessResultWrapperByMsg("修改客户状态配置成功");
    }

    /**
     * 客户状态配置 删除
     *
     * @param id ID
     * @return ResultVo
     */
    @ApiOperation(value = "删除客户状态配置数据", notes = "删除客户状态配置数据")
    @PreAuthorize("hasAuthority('tenant_customerstatus_delete')")
    @OperateLogger(description = "删除客户状态配置数据", module = ModuleEnum.MODULE_TENANT, operationType = OperationTypeEnum.DELETE, db = true)
    @Override
    public ResultWrapper<?> del(String id) {
        IService.delete(id);
        return ResultWrapper.getSuccessResultWrapperByMsg("删除客户状态配置成功");
    }

    /**
     * 客户状态配置 批量删除
     *
     * @param ids ID 数组
     * @return ResultVo
     */
    @ApiOperation(value = "批量删除客户状态配置数据", notes = "批量删除客户状态配置数据")
    @PreAuthorize("hasAuthority('tenant_customerstatus_delete')")
    @OperateLogger(description = "批量删除客户状态配置数据", module = ModuleEnum.MODULE_TENANT, operationType = OperationTypeEnum.DELETE, db = true)
    @Override
    public ResultWrapper<?> delAll(String ids) {
        String[] idArray = Convert.toStrArray(ids);
        IService.deleteAll(idArray);
        return ResultWrapper.getSuccessResultWrapperByMsg("批量删除客户状态配置成功");
    }

    /**
     * 客户状态配置 Excel 导出认证
     */
    @ApiOperation(value = "Excel 导出认证", notes = "Excel 导出认证")
    @PreAuthorize("hasAnyAuthority('tenant_customerstatus_export', 'tenant_customerstatus_import')")
    @Override
    public ResultWrapper<String> exportExcelAuth(String type, HttpServletRequest request) {
        Optional<String> certificateOptional = super.excelExportAuth(type, TenantCustomerStatusRestApi.SUB_TITLE,
                request);
        if (!certificateOptional.isPresent()) {
            return ResultWrapper.getErrorResultWrapper();
        }
        return ResultWrapper.getSuccessResultWrapper(certificateOptional.get());
    }

    /**
     * 客户状态配置 Excel 导出
     */
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    @PreAuthorize("hasAuthority('tenant_customerstatus_export')")
    @OperateLogger(description = "客户状态配置 导出Excel", module = ModuleEnum.MODULE_TENANT, operationType = OperationTypeEnum.SELECT, db = true)
    @Override
    public void exportExcel(String certificate, HttpServletResponse response) {
        super.excelExport(certificate, response);
    }

    /**
     * 客户状态配置 Excel 导入
     */
    @ApiOperation(value = "导入Excel", notes = "导入Excel")
    @PreAuthorize("hasAuthority('tenant_customerstatus_import')")
    @OperateLogger(description = "客户状态配置 Excel 导入", module = ModuleEnum.MODULE_TENANT, operationType = OperationTypeEnum.INSERT, db = true)
    @Override
    public ResultWrapper<?> importExcel(MultipartHttpServletRequest request) {
        return super.importExcel(request);
    }
}